<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="活动名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入活动名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地点" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入地点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主办方" prop="organizer">
        <el-input
          v-model="queryParams.organizer"
          placeholder="请输入主办方"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="承办方" prop="contractor">
        <el-input
          v-model="queryParams.contractor"
          placeholder="请输入承办方"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间" prop="noticeType">
        <el-date-picker
          v-model="dateRange"
          style="width: 350px"
          value-format="yyyy-MM-dd HH:mm"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <template>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['psy:activity:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['psy:activity:edit']"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['psy:activity:remove']"
            >删除</el-button
          >
        </el-col>
      </template>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="noticeList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index"  label="序号" align="center" />
      <el-table-column
        label="活动名称"
        align="center"
        prop="name"
        :show-overflow-tooltip="true"
      />
      <!-- <el-table-column label="公告内容" align="center" prop="noticeContent" :show-overflow-tooltip="true" /> -->
      <el-table-column
        label="地点"
        align="center"
        prop="location"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="时间"
        align="center"
        prop="startEndTimeStr"
        width="240"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <!-- <el-table-column
        label="活动简介"
        align="center"
        prop="description"
        :show-overflow-tooltip="true"
      >
      </el-table-column> -->
      <el-table-column
        label="主办方"
        align="center"
        prop="organizer"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="承办方"
        align="center"
        :show-overflow-tooltip="true"
        prop="contractor"
      >
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        sortable
        :show-overflow-tooltip="true"
        prop="createTime"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-hasPermi="['psy:activity:query']"
            @click="handleDetails(scope.row)"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row, 'edit')"
            v-hasPermi="['psy:activity:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['psy:activity:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="960px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="活动名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入活动名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="活动地点" prop="location">
              <el-input v-model="form.location" placeholder="请输入活动地点" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="活动时间" prop="timeRange">
              <el-date-picker
                v-model="form.timeRange"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd HH:mm"
                format="yyyy-MM-dd HH:mm"
                @input="handleTimeChange"
                :picker-options="pickerOptions"
              ></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主办方" prop="organizer">
              <el-input v-model="form.organizer" placeholder="请输入主办方" />
            </el-form-item>
          </el-col>
          <el-col :span="13">
            <el-form-item label="承办方" prop="contractor">
              <el-input v-model="form.contractor" placeholder="请输入承办方" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="活动简介" prop="description">
              <editor v-model="form.description" :min-height="150" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="submit"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 查看详情 -->
    <el-dialog :title="title" :visible.sync="openDetails" width="960px">
      <el-form :model="detailsForm" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="活动名称:" prop="name">
              {{  detailsForm.name}}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="活动地点:" prop="location">
              {{ detailsForm.location }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="活动时间:" prop="timeRange">
              {{detailsForm.startEndTimeStr  }}
             
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主办方:" prop="organizer">
              {{detailsForm.organizer  }}
            </el-form-item>
          </el-col>
          <el-col :span="13">
            <el-form-item label="承办方:" prop="contractor">
              {{detailsForm.contractor  }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="活动简介:" prop="description">
              <div v-html="detailsForm.description"></div>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
    <!-- <SelectUser ref="selectUserRef" :sendType="form.sendType" :sendUserIds.sync="sendUserIds"></SelectUser>
          <ReadStatusUser ref="readStatusUserRef"></ReadStatusUser> -->
  </div>
</template>
      
      <script>
import {
  activityList,
  getActivity,
  activityAdd,
  updataActivity,
  activityDel,
} from "@/api/psychological/eventReleasee.js";
// import SelectUser from "./moudles/selectUser";
// import ReadStatusUser from "./moudles/readStatusUser";
import * as dayjs from "dayjs";
import { titleCase } from '../../utils';
export default {
  name: "Notice",
  dicts: ["sys_notice_status", "sys_notice_type"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      submit: false,
      openDetails:false,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        organizer: "",
        name: "",
        location: "",
        contractor: "",
      },
      pickerOptions: {
        // 禁用过去时间（可选）
        disabledDate(time) {
          return time.getTime() < Date.now() - 86400000; // 禁用今天之前
        },
      },
      // 表单参数
      form: {
        timeRange: [],
      },
      detailsForm:{},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "活动名称不能为空", trigger: "blur" },
        ],
        location: [
          { required: true, message: "活动地点不能为空", trigger: "blur" },
        ],
        timeRange: [
          { required: true, message: "活动时间不能为空", trigger: "change" },
        ],
        organizer: [
          { required: true, message: "主办方不能为空", trigger: "blur" },
        ],
        contractor: [
          { required: true, message: "承办方不能为空", trigger: "blur" },
        ],
        description: [
          { required: true, message: "活动简介不能为空", trigger: "blur" },
        ],
      },
    };
  },

  created() {
    this.getList();
  },
  computed: {},
  methods: {
    handleTimeChange(value) {
      console.log(value, "c");
      this.$nextTick(() => {
        this.$forceUpdate();
      });
    },
    // 发送给后台的数据处理
    getSubmitData() {
      if (this.form.timeRange.length === 2) {
        return {
          startTime: this.form.timeRange[0],
          endTime: this.form.timeRange[1],
        };
      }
      return {}; // 时间未选择时返回空
    },
    /** 查询公告列表 */
    getList() {
      this.loading = false;
      // if(this.dateRange){
      //   this.queryParams.beginTime = this.dateRange[0]
      //   this.queryParams.endTime = this.dateRange[0]

      // }
      activityList(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.noticeList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        location: undefined,
        startTime: undefined,
        endTime: undefined,
        contractor: undefined,
        description: undefined,
        organizer: undefined,
        time: undefined,
      };
      this.resetForm("form");
    },
    changeTiem(data) {
      console.log(data, "data");
      this.form.tiem = data;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.dateRange = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.paId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动";
    },
    /** 修改按钮操作 */
    handleUpdate(row, type) {
      // this.reset();
      const noticeId = row.paId;
      getActivity(noticeId).then((response) => {
        this.form = response.data;

        if (response.data.startTime && response.data.endTime) {
          // 转换为组件需要的格式
          this.form.timeRange = [
            response.data.startTime, // 直接使用后台返回的字符串
            response.data.endTime,
          ];
          this.$set(this.form.timeRange, 0, response.data.startTime); // 更新开始时间
          this.$set(this.form.timeRange, 1, response.data.startTime);
          this.form.timeRange = [...this.form.timeRange];
        }

        console.log(this.form.timeRange, "his.form.time ");
        this.open = true;
        if (type == "detail") {
          this.title = "查看活动";
          this.submit = true;
        } else {
          this.title = "修改活动";
          this.submit = false;
        }
      });
    },
    handleDetails(row) {
      console.log(row,'查看详情')
      this.openDetails = true
      this.title = '查看详情'
      this.detailsForm = row
    },
    /** 提交按钮 */
    submitForm: function () {
      const params = this.getSubmitData();
      this.form = {
        ...this.form,
        startTime: params.startTime,
        endTime: params.endTime,
      };
      console.log(this.form, "this.form");
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.paId != undefined) {
            updataActivity(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            activityAdd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noticeIds = row.paId || this.ids;
      this.$modal
        .confirm('是否确认删除公告编号为"' + noticeIds + '"的数据项？')
        .then(function () {
          return activityDel(noticeIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
      <style lang="scss" scoped>
.readStatus {
  cursor: pointer;
  &-readCount {
    color: #67c23a;
  }
  &-notReadCount {
    color: #909399;
  }
}
</style>